package cn.simon.controller;

import cn.simon.pojo.User;
import cn.simon.service.Impl.GoodsServiceImpl;
import cn.simon.service.Impl.UserServiceImpl;

import cn.simon.vo.DetailVo;
import cn.simon.vo.GoodsVo;
import cn.simon.vo.RespBean;
import lombok.extern.java.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.thymeleaf.context.WebContext;
import org.thymeleaf.spring5.view.ThymeleafViewResolver;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Log
@Controller
@RequestMapping("/goods")
public class GoodsController {

    @Autowired
    private UserServiceImpl userService;
    @Autowired
    private GoodsServiceImpl goodsService;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private ThymeleafViewResolver viewResolver;

    @RequestMapping(value = "/toList",produces = "text/html;charset=utf-8")
    @ResponseBody
    public String toGoods( Model model,User user,
                           HttpServletRequest request, HttpServletResponse response){
        //查看redis中，是否又页面goodsList缓存
        ValueOperations operations = redisTemplate.opsForValue();
        String html = (String) operations.get("goodsList");
        /*String html = (String) redisTemplate.opsForValue().get("goodsList");*/

        //判断页面是够存在
        if(!StringUtils.isEmpty(html)){
            return html;
        }
        model.addAttribute("user",user);
        model.addAttribute("goodsList",goodsService.findGoodsVo());

        //如果没有，手动渲染Redis，并返回页面
        WebContext webContext = new WebContext(request,response,
                request.getServletContext(),request.getLocale(),
                model.asMap());
        html = viewResolver.getTemplateEngine().process("goodsList",webContext);
        if(!StringUtils.isEmpty(html)){
            //加载到redis,设置失效时间为一分钟
            operations.set("goodsList",html,60, TimeUnit.SECONDS);
        }
        return html;

    }


    /**
     * @功能描述: 商品详情，使用 “静态页面”
     * @Param:
     * @Return:
     * @Author suxiangshan
     * @Date 2022/11/2 15:12
     */
    @RequestMapping("/detail/{goodsId}")
    @ResponseBody
    public RespBean toDetail(User user, @PathVariable Long goodsId) {
        GoodsVo goodsVo = (GoodsVo) goodsService.findGoodsVoById(goodsId);
        Date startDate = goodsVo.getStartDate();
        Date endDate = goodsVo.getEndDate();
        Date nowDate = new Date();
        //秒杀状态
        int secKillStatus = 0;
        //秒杀倒计时
        int remainSeconds = 0;
        //秒杀还未开始
        if (nowDate.before(startDate)) {
            remainSeconds = ((int) ((startDate.getTime() - nowDate.getTime()) / 1000));
        } else if (nowDate.after(endDate)) {
            //	秒杀已结束
            secKillStatus = 2;
            remainSeconds = -1;
        } else {
            //秒杀中
            secKillStatus = 1;
            remainSeconds = 0;
        }
        log.info("-----------------remainSeconds:"+remainSeconds);
        DetailVo detailVo = new DetailVo();
        detailVo.setUser(user);
        detailVo.setGoodsVo(goodsVo);
        detailVo.setSecKillStatus(secKillStatus);
        detailVo.setRemainSeconds(remainSeconds);
        log.info("-----------------"+detailVo);

        return RespBean.success(detailVo);
    }


}
